package com.birth.memo.util;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;




public class DateUtil {
	
	private static final String TAG = "DateUtil";

   
    public static String formatDate(String time, String fromFormat,
            String toFormat)  {
    	if (time == null || "".equals(time.trim()))
    		return null;
        String dateTime = time;

        SimpleDateFormat oldFormat = new SimpleDateFormat(fromFormat);
        SimpleDateFormat newFormat = new SimpleDateFormat(toFormat);

        String dataStr = null;

        try {
			dataStr = newFormat.format(oldFormat.parse(dateTime));
		} catch (ParseException e) {
			return null;
		}

        return dataStr;
    }
    
    public static String formatDate(String time,String toFormat){
    	if (time == null || "".equals(time.trim()))
    		return "";
    	
    	String fromFormat = null;
    	if(time.length() == 6){
    		fromFormat = "HHmmss";
    	}else if(time.length()==8){
    		fromFormat = "yyyyMMdd";
    	}else if(time.length() == 14){
    		fromFormat = "yyyyMMddHHmmss";
    	}else{
    		return time;
    	}
    	SimpleDateFormat oldFormat = new SimpleDateFormat(fromFormat);
        SimpleDateFormat newFormat = new SimpleDateFormat(toFormat);
        try {
			return newFormat.format(oldFormat.parse(time));
		} catch (ParseException e) {
			Log.e(TAG,"date format error:"+time);
			return time;
		}
    }
    
    public static String formatDate(long time,String toFormat){
    	Date date = new Date(time);
    	SimpleDateFormat newFormat = new SimpleDateFormat(toFormat);
    	return newFormat.format(date);
    }

    /**
     * @author 
     * @time 10:43:46 AM
     * @return
     * @explanation get current date string meeting formatStr
     */
    public static String getCurrentDate(String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(new Date());
    }

    /**
     * @author 
     * @time 10:43:46 AM
     * @return
     * @explanation format date according to toFormat
     */
    public static String formatDate(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }
    
    public static String getDesc(int duration){
		String desc = "";
		if(duration>0){
        	if(duration<60){
        		desc = duration+"秒";
        	}else{
        		desc = duration/60+"分"+duration%60+"秒";
        	}
        }else{
        	desc = "0";
        }
		return desc;
	}
    
    public static long cal(String stdate, String endate) {
		long jgts = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
		try {
			Date date1 = ft.parse(stdate);
			Date date2 = ft.parse(endate);
			jgts = date1.getTime() - date2.getTime();
			jgts = jgts / 1000 / 60 / 60 / 24;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return jgts;
	}
   
	public static void main(String args[]){
//		long interval = DateUtil.getInterval("16:00:00");
//		System.out.println(interval);
		
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -3);
		System.out.println( formatDate(calendar.getTime(),"yyyyMMddHHmmss"));
	}

    
}
